package com.dy.controller;

import com.dy.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.util.List;

/**
 * @ClassName:
 * @Author: 仄言in
 * @Desctiption: (描述)
 * @Date: 2021/4/8 13:27
 * @Version 1.0 (版本)
 */
@RestController
public class RestUserController {
    @Autowired
    private RestTemplate restTemplate;
    //private final static String REST_BEFORE="http://localhost:8081";
    private final static String REST_BEFORE="http://SPRINGCLOUD-PROVIDER";
    @RequestMapping("/dy/user/select")
    public List<User> select(){
        System.out.println("消费者");
        List<User> selects =restTemplate.getForObject("http://SPRINGCLOUD-PROVIDER" + "/select", List.class);
        return selects;
    }
    @RequestMapping("/dy/user/insert")
    public String insert(@RequestBody User user){
        restTemplate.postForObject(REST_BEFORE + "/insert",user, String.class);
        return "insert";
    }
    @RequestMapping("/dy/user/delete/{uid}")
    public String delete(@PathVariable("uid") Integer uid){
        restTemplate.delete(REST_BEFORE+"/delete/"+uid);
        return "delete";
    }
    @RequestMapping("/dy/user/update")
    public String update(@RequestBody User user){
        restTemplate.put(REST_BEFORE+"/update",user);
        return "update";
    }
    @RequestMapping("dy/user/selectById/{uid}")
    public User selectById(@PathVariable Integer uid){
        User users = restTemplate.getForObject(REST_BEFORE + "/selectById/" + uid, User.class);
        return users;
    }
}
